<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 9/17/14
 * Time: 8:15 AM
 */
namespace BC;

use CPath\Autoloader;
use CPath\Build\IBuildable;
use CPath\Build\IBuildRequest;
use CPath\Data\Ledger\JSON\ILedger;
use CPath\Request\IRequest;
use CPath\Request\Request;
use CPath\Route\CPathMap;
use CPath\Route\IRouteMap;
use CPath\Route\IRouteMapper;
use CPath\Route\RouteBuilder;
use CPath\Route\RouteRenderer;

require_once(__DIR__ . '/CPath/Autoloader.php');
Autoloader::addLoader('BC', __DIR__);

class SiteIndex implements IRouteMap, IBuildable
{

//	/**
//	 * Execute a command and return a response. Does not render
//	 * @param \CPath\Request\Form\IFormRequest|\CPath\Request\IRequest $Request the request prompt
//	 * @return IResponse the execution response
//	 */
//    function execute(IRequest $Request) {
//        return new RouteIndexResponse(new SiteIndex());
//    }

    /**
     * Maps all routes to the route map. Returns true if the route prefix was matched
     * @param IRouteMapper $Map
     * @return bool if true the route prefix was matched, otherwise false
     * @build routes --disable 0
     * Note: Set --disable 1 or remove doc tag to stop code auto-generation on build for this method
     */
    function mapRoutes(IRouteMapper $Map) {
		return
			// @group _poll
			$Map->route('/poll', 'BC\\Misc\\LongPoll') ||
//
			// @group BC\Games\Holdem\HoldemInvite
			$Map->route('ANY /games/holdem/invite', 'BC\\Games\\Holdem\\HoldemInvite') ||

			// @group BC\Games\Holdem\HoldemRoute
			$Map->route('ANY /games/holdem/', 'BC\\Games\\Holdem\\HoldemRoute') ||

			// @group BC\Games\Holdem\HoldemTable
			$Map->route('ANY /games/holdem/table/:id/', 'BC\\Games\\Holdem\\HoldemTable') ||

			// @group BC\Render\DefaultTemplate
			$Map->route('ANY *', 'BC\\Render\\DefaultTemplate') ||

			// @group BC\User\Login
			$Map->route('ANY /login/:user', 'BC\\User\\Login', 32) ||
			$Map->route('ANY /user/:user/login', 'BC\\User\\Login') ||

			// @group BC\User\Logout
			$Map->route('ANY /logout/', 'BC\\User\\Logout', 16) ||
			$Map->route('ANY /user/:user/logout', 'BC\\User\\Logout') ||
			$Map->route('ANY /user/:fingerprint/logout', 'BC\\User\\Logout') ||

			// @group BC\User\Register
			$Map->route('ANY /register', 'BC\\User\\Register') ||
//
//			// @group BC\User\Search
//			$Map->route('ANY /users/:search/', 'BC\\User\\Search') ||
//			$Map->route('ANY /users/', 'BC\\User\\Search') ||

			// @group BC\User\Session\UserSession
			$Map->route('ANY /session/:fingerprint/:command', 'BC\\User\\Session\\UserSession') ||
//
//			// @group BC\User\UserRoute
//			$Map->route('ANY /user/:name', 'BC\\User\\UserRoute') ||
//			$Map->route('ANY /profile', 'BC\\User\\UserRoute') ||

			// @group BC\Wallet\Bitcoin\BitcoinUserWallets
			$Map->route('ANY /wallets/', 'BC\\Wallet\\Bitcoin\\BitcoinUserWallets') ||
			$Map->route('ANY /user/:user/wallets', 'BC\\Wallet\\Bitcoin\\BitcoinUserWallets') ||

			// @group BC\Wallet\Bitcoin\BitcoinWalletCreate
			$Map->route('ANY /wallets/create', 'BC\\Wallet\\Bitcoin\\BitcoinWalletCreate') ||
			$Map->route('POST /wallets/create', 'BC\\Wallet\\Bitcoin\\BitcoinWalletCreate') ||

			// @group BC\Wallet\Bitcoin\BitcoinWallet
			$Map->route('ANY /wallet/:fingerprint', 'BC\\Wallet\\Bitcoin\\BitcoinWallet') ||
			$Map->route('ANY /wallet/:fingerprint/:command', 'BC\\Wallet\\Bitcoin\\BitcoinWallet') ||
			$Map->route('ANY /wallet/:fingerprint:command', 'BC\\Wallet\\Bitcoin\\BitcoinWallet') ||


			// @group __map_html
			$Map->route('ANY *', 'BC\\Render\\CustomHTMLMapRenderer') ||

			// @group _cpath
			$Map->route('ANY *', new CPathMap());
	}

    // Static

//    /**
//     * Handle this request and render any content
//     * @param IBuildRequest $Request the build request inst for this build session
//     * @return void
//     * @build --disable 0
//     * Note: Use doctag 'build' with '--disable 1' to have this IBuildable class skipped during a build
//     */
//    static function handleStaticBuild(IBuildRequest $Request) {
//        $RouteBuilder = new RouteBuilder($Request, new SiteIndex());
//        $RouteBuilder->writeRoute('ANY /', __CLASS__);
//    }

    /**
     * Handle this request and render any content
     * @param IRequest $Request the IRequest inst for this render
     * @return bool returns true if the route was rendered, false if no route was matched
     */
    static function route(IRequest $Request=null) {
        if(!$Request)
            $Request = Request::create();

        $Renderer = new RouteRenderer($Request);
	    $Index = new SiteIndex;
	    return $Renderer->renderRoutes($Index);
    }

	/**
	 * Handle this request and render any content
	 * @param IBuildRequest $Request the build request inst for this build session
	 * @return void
	 * @build --disable 0
	 * Note: Use doctag 'build' with '--disable 1' to have this IBuildable class skipped during a build
	 */
	static function handleBuildStatic(IBuildRequest $Request) {
		$RouteBuilder = new RouteBuilder($Request, new static, '_cpath');
		$RouteBuilder->writeRoute('ANY *', 'new CPathMap()');
	}
}